package com.itboye.mengwu.app.ui.main;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.ProductAskModel;
import com.itboye.mengwu.data.presenter.ProductAskPresenter;
import com.itboye.mengwu.goodsdetail.snapscrollview.McoySnapPageLayout.McoySnapPage;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class FragmentProductASk extends BaseFragment implements Observer,
		McoySnapPage {
	// IXListViewListener {
	// XListView query_list;
	ListView query_list;
	String pid;
	ProductAskPresenter askPresenter;
	ProductAskModel askModel;
	ProductAskModel ask;
	int pageNo = 1;
	String type = "load";
	View view;
	TextView ask_click;
	private boolean isAtTop;
	private boolean isIdle;
	List<ProductAskModel.Product> list = new ArrayList<ProductAskModel.Product>();

	public FragmentProductASk(String pid) {
		// TODO Auto-generated constructor stub
		this.pid = pid;
	}

	public FragmentProductASk() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_productask;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		askModel = new ProductAskModel();
		askPresenter = new ProductAskPresenter(this);
		askModel.setPid(pid);
		askModel.setPage_size(1000);
		askPresenter.getInfo(askModel);
		query_list.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				isIdle = scrollState == OnScrollListener.SCROLL_STATE_IDLE;
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				Log.e("log", "firstVisibleItem is " + firstVisibleItem);
				if (firstVisibleItem == 0 && isIdle) {
					Log.e("hu log", "滑到顶部");
					isAtTop = true;
				}
				if (visibleItemCount + firstVisibleItem == totalItemCount) {
					Log.e("hu log", "滑到底部");
				}
			}
		});

		view = LayoutInflater.from(getActivity()).inflate(
				R.layout.product_asksave, null);
		ask_click = (TextView) view.findViewById(R.id.ask_click);
		ask_click.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), AcitvityBuyAsk.class);
				intent.putExtra("pid", pid);
				startActivity(intent);
			}
		});
		query_list.addHeaderView(view);

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			ask = new ProductAskModel();
			ask = (ProductAskModel) entity.getData();
			MyAdapter adapter = new MyAdapter(getActivity());
			query_list.setAdapter(adapter);

			// askModel.setPid(pid);
			// askModel.setPage_size(1000);
		}
		// askPresenter.getInfo(askModel);
	}

	class MyAdapter extends BaseAdapter {
		Context context;

		public MyAdapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;

		}

		public MyAdapter() {
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return ask.getList().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@SuppressWarnings("unused")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (holder == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_productask, null);
				holder.wangyou_name = (TextView) convertView
						.findViewById(R.id.wangyou_name);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.query = (TextView) convertView.findViewById(R.id.query);
				holder.answer = (TextView) convertView
						.findViewById(R.id.answer);
				SimpleDateFormat myFmt1 = new SimpleDateFormat(
						"yyyy年MM月dd日 HH:mm:ss");

				String positionDay = myFmt1.format(Long.parseLong((ask
						.getList().get(position).getAsk_time()) + "") * 1000);
				holder.time.setText(positionDay);
				holder.wangyou_name.setText(ask.getList().get(position)
						.getAsk_nickname());
				holder.query.setText("咨询内容："
						+ ask.getList().get(position).getAsk_content());
				holder.answer.setText("萌屋回复："
						+ ask.getList().get(position).getReply_content());
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			return convertView;
		}
	}

	class ViewHolder {
		TextView wangyou_name, query, answer, time;
	}

	@Override
	public View getRootView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isAtTop() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isAtBottom() {
		// TODO Auto-generated method stub
		return isAtTop;
	}

	

	// @Override
	// public void onRefresh() {
	// // TODO Auto-generated method stub
	// type = "refresh";
	// pageNo = 1;
	// askModel.setPid(pid);
	// askModel.setPage_no(pageNo + "");
	// askPresenter.getInfo(askModel);
	// }

	// @Override
	// public void onLoadMore() {
	// // TODO Auto-generated method stub
	//
	// type = "load";
	// pageNo++;
	// askModel.setPid(pid);
	// askModel.setPage_no(pageNo + "");
	// askPresenter.getInfo(askModel);
	// }

	// public void stopLoad() {
	// query_list.stopLoadMore();
	// query_list.stopRefresh();
	// }

}
